﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class InsertPoint : Operation
	{
        Engine.Selection Selected;
		Vector2 Point;
        Data.EditorPolygon Polygon;
        int InsertIndex;

        internal InsertPoint(Engine.Selection Selected, Vector2 Point)
		{
			this.Selected = Selected;
			this.Point = Point;
            Polygon = null;
            InsertIndex = 0;
		}

        internal override ReturnCode Do(EditorState ES)
        {
            Polygon = ES.GetMiddleObject(ES.Selected) as Data.EditorPolygon;
            if (Polygon == null) return ReturnCode.DoNotAddToUndoStack;
            if (Selected.Type == Jemgine.Engine.Selection.Types.Vertex) InsertIndex = Selected.BottomIndex;
            Polygon.Insert(InsertIndex, new Jemgine.Data.Vertex(Point));
            Polygon.ClearDecomposedCache();
            return ReturnCode.AddToUndoStack;
        }

        internal override void Undo(EditorState ES)
        {
            Polygon.RemoveAt(InsertIndex);
        }
	}
}
